package com.twm.tamauth.utils

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.media.projection.MediaProjectionManager
import android.os.Build
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView
import com.twm.tamauth.R
import com.twm.tamauth.adapter.MyAuthItemAdapter
import com.twm.tamauth.databinding.ListactivityMainBinding
import com.twm.tamauth.model.data.Datasource
import com.twm.tamauth.service.FloatDisplayCallback
import com.twm.tamauth.service.MyNotificationService

/**请求媒体投影权限的常量*/
const val REQUEST_CODE = 1001

@SuppressLint("WrongConstant")
class MyTransparentActivity : AppCompatActivity(), MyAuthItemAdapter.ItemClickListener,
    FloatDisplayCallback {

    private lateinit var binding: ListactivityMainBinding
    private var myOrderId: String = ""
    private var myGameId: String = ""
    private var myNotificationService: MyNotificationService? = null

    @RequiresApi(Build.VERSION_CODES.O)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        overridePendingTransition(0, 0)
        setStatusBar()
        bindUI()
        populateData()
        setupListeners()
    }

    /**
     * 全屏设置
     */
    private fun setStatusBar() {
        window.statusBarColor = Color.WHITE
        window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
    }

    /**
     * 绑定布局
     */
    private fun bindUI() {
        binding = ListactivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
    }

    private fun populateData() {
        extractDataFromIntent()
        setupRecyclerView()
    }

    /**
     * 获取传入的订单数据
     */
    private fun extractDataFromIntent() {
        intent.getStringExtra("orderId")?.let { myOrderId = it }
        intent.getStringExtra("gameId")?.let { myGameId = it }
    }

    /**
     * 设置cycle view
     */
    private fun setupRecyclerView() {
        val myDataset = Datasource().loadAffirmations()
        val recyclerView = findViewById<RecyclerView>(R.id.recycler_view)

        recyclerView.adapter = MyAuthItemAdapter(this, myDataset)
        recyclerView.setHasFixedSize(true)
        (recyclerView.adapter as? MyAuthItemAdapter)?.setItemClickListener(this)
    }

    /**
     * 设置cycle list item 点击
     */
    override fun onItemClick(itemText: String) {
        // 处理item点击事件
    }

    /**<设置按钮监听>*/
    @RequiresApi(Build.VERSION_CODES.O)
    private fun setupListeners() {
        binding.nextButton.setOnClickListener {
            startScreenCaptureRequest()
        }

        binding.backButton.setOnClickListener {
            finish()
        }
    }

    /**
     * 请求录屏权限
     */
    private fun startScreenCaptureRequest() {
        val mediaProjectionManager = getSystemService(Context.MEDIA_PROJECTION_SERVICE) as MediaProjectionManager
        val screenCaptureIntent = mediaProjectionManager.createScreenCaptureIntent()
        startActivityForResult(screenCaptureIntent, REQUEST_CODE)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
            val intent = Intent(this, MyNotificationService::class.java).apply {
                putExtra("resultCode", resultCode)
                putExtra("data", data)
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                startForegroundService(intent)
            } else {
                startService(intent)
            }
        }
    }

    /**
     * 打开游戏
     */
    private fun launchGame() {
        packageManager.getLaunchIntentForPackage(myGameId)?.let {
            try {
                // 启动游戏
                startActivity(it)
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }


    //MARK: - FloatDisplayCallback
    override fun onFloatDisplayed() {
        // 浮窗显示成功后的回调处理
        Toast.makeText(this, "浮窗显示成功", Toast.LENGTH_SHORT).show()
    }

}
